﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Faust.Andrew.MTG.Player
{
	/// <summary>
	/// Interaction logic for PlayersControl.xaml
	/// </summary>
	public partial class PlayersControl : UserControl, IPlayersView
	{
        static PlayersControl()
        {
            PresenterProperty = DependencyProperty.Register("Presenter", typeof(GamePresenter), typeof(PlayersControl));
        }

        static DependencyProperty PresenterProperty;
        public GamePresenter Presenter
        {
            get
            {
                return (GamePresenter)base.GetValue(PresenterProperty);
            }
            set
            {
                base.SetValue(PresenterProperty, value);
            }
        }

        public PlayersControl()
		{
			this.InitializeComponent();
		}

        public void RefreshPlayers()
        {
            playerItemsControl.DataContext = Presenter.Model;
        }

        private void btnAddLife_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            Shared.Player player = ((sender as Button).DataContext as PlayerState);
            if (Presenter.Model.CanUpdatePlayer(player))
            {
                player.LifeCounter++;
            }            
        }

        private void btnRemoveLife_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            Shared.Player player = ((sender as Button).DataContext as PlayerState);
            if (Presenter.Model.CanUpdatePlayer(player))
            {
                player.LifeCounter--;
            }
        }

        private void EditCounters_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            Shared.Player player = ((sender as FrameworkElement).DataContext as PlayerState);
            if (Presenter.Model.CanUpdatePlayer(player))
            {
                CounterEditor counterEditor = new CounterEditor();
                foreach (Shared.Counter counter in player.Counters)
                {
                    counterEditor.Counters.Add((Shared.Counter)counter.Clone());
                }

                if (counterEditor.ShowDialog() == true)
                {
                    player.Counters = counterEditor.Counters;
                }
            }
        }

        private void Player_PreviewDrop(object sender, System.Windows.DragEventArgs e)
        {
            FrameworkElement element = sender as FrameworkElement;

            if (element != null && element.DataContext is PlayerState)
            {
                Presenter.DropOnPlayer((PlayerState)element.DataContext, e);
            }
        }        
	}
}